<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :search.sync="search"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
      <template slot="menuLeft">
        <el-button type="warning" size="small" icon="el-icon-download" @click="$refs.exportComp.visible = true">导出</el-button>
      </template>
      <template slot-scope="{ row }" slot="menu">
        <el-button icon="el-icon-view" size="small" type="text" :disabled="row.isForSystem" @click="viewDetail(row)">工单详情</el-button>
      </template>
      <template slot="partnerNameSearch">
        <el-input style="width: 100%" disabled v-model="search.partnerName"></el-input>
      </template>
    </avue-crud>
    <el-dialog class="detailDialog" append-to-body :close-on-click-modal="false" title="工单详情" :visible.sync="handleDialog">
      <!-- <div class="titleIcon">
        <i class="el-icon-vertical-bar avue-group__icon"></i>
        <h4>基本信息</h4>
      </div> -->
      <!-- <avue-form v-model="basicInfoForm" :option="basicInfoOption"></avue-form> -->
      <avue-crud v-if="handleDialog" :row-class-name="tableRowClassName" @row-click="rowClick" :option="workOrderNoOption" :data="workOrderNoData"></avue-crud>
      <div class="titleIcon">
        <!-- <i class="el-icon-vertical-bar avue-group__icon"></i> -->
        <h4>工单：{{ workOrderNoModel.workOrderNo }}生产进度</h4>
      </div>
      <avue-crud v-if="handleDialog" :option="stationCrossingOption" :data="stationCrossingData"></avue-crud>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleDialog = false"><i class="el-icon-circle-close"></i><span>取 消</span></el-button>
      </span>
    </el-dialog>
    <export-comp
      ref="exportComp"
      page-api-url="mes-abacus/production-order-report/page-query/production-order-progress"
      :option="option"
      :page="page"
      :query="query"
      file-name="订单进度查询"
    >
    </export-comp>
  </basic-container>
</template>

<script>
import { getList, getWorkOrderNoInfo, getOverStationInfo, getUserInfo } from '@/api/product/orderProgressQuery';
import { mapGetters } from 'vuex';
import CommonModalTemplate from '@/components/common-modal-template/common-modal-template.vue';
// import { getList as getProductionlineList } from '@/api/product/productionline';
import userTableConfig from '@/components/user-table-config';
import userConfig from '@/mixins/userConfig';
import exportComp from '@/components/export-comp';
import moment from 'moment';
export default {
  name: 'ProductProductProgressQuery',
  components: {
    CommonModalTemplate,
    userTableConfig,
    exportComp
  },
  mixins: [userConfig()],
  data() {
    return {
      pageCode: 'PRODUCT_PROGRESS_QUERY',
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      search: {
        planEndDate: [moment().format('YYYY-MM-DD 00:00:00'), moment().add(30, 'day').format('YYYY-MM-DD 23:59:59')]
      },
      selectionList: [],
      workOrderNoData: [],
      workOrderNoModel: {},
      option: {
        height: 'calc(100vh - 483px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchSpan: 6,
        searchMenuSpan: 16,
        labelWidth: 100,
        searchLabelWidth: 90,
        border: true,
        index: true,
        editBtn: false,
        viewBtn: false,
        selection: true,
        delBtn: false,
        addBtn: false,
        dialogClickModal: false,
        indexLabel: '序号',
        searchMenuPosition: 'right',
        columnBtn: false,
        menuWidth: 100,
        column: [
          {
            label: '客户名称',
            prop: 'partnerName',
            search: true,
            searchOrder: 1,
            disabledSearch: true
            // disabledsearch: true
          },
          {
            label: '生产订单',
            prop: 'productionOrderNo',
            search: true,
            searchOrder: 2
          },
          {
            label: '批号',
            prop: 'lotNo',
            search: true,
            searchOrder: 7
          },
          {
            label: '订单数量',
            prop: 'productionOrderQuantity'
          },
          {
            label: '已发货数量',
            prop: 'deliverQuantity'
          },
          {
            label: '待发货数量',
            prop: 'waitDeliveryQuantity'
          },
          {
            label: '产品编码',
            prop: 'partNo',
            search: true,
            searchOrder: 3
          },
          {
            label: '产品名称',
            prop: 'partName',
            search: true,
            searchOrder: 4
          },
          {
            label: '产品型号',
            prop: 'partModel',
            search: true,
            searchOrder: 4
          },

          {
            label: '计划开始时间',
            prop: 'planStartDate'
          },
          {
            label: '计划结束时间',
            prop: 'planEndDate',
            search: true,
            searchLabelWidth: 170,
            searchSpan: 8,
            searchType: 'datetimerange',
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            searchLabel: '计划结束时间查询范围',
            startPlaceholder: '开始时间',
            endPlaceholder: '结束时间',
            searchRules: [
              {
                required: true,
                message: '请输入计划结束时间查询范围',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '销售订单',
            prop: 'salesOrderNo',
            search: true,
            searchOrder: 2
          },
          {
            label: '交货时间',
            prop: 'deliveryDate'
          },
          {
            label: '最后同步时间',
            prop: 'lastSyncTime'
          }
        ]
      },
      data: [],
      handleDialog: false,
      basicInfoForm: {},
      workOrderNoOption: {
        height: 200,
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        editBtn: false,
        viewBtn: false,
        selection: false,
        delBtn: false,
        addBtn: false,
        indexLabel: '序号',
        columnBtn: false,
        refreshBtn: false,
        menu: false,
        column: [
          {
            label: '工单号',
            prop: 'workOrderNo'
          },
          {
            label: '产品编码',
            prop: 'partNo'
          },
          {
            label: '产品名称',
            prop: 'partName'
          },
          {
            label: '产品型号',
            prop: 'partModel'
          },
          {
            label: '工单计划数量',
            prop: 'planQuantity'
          },
          {
            label: '状态',
            prop: 'status',
            type: 'select',
            html: true,
            formatter: (val) => this.$statusText.GD(val.status)
          },
          {
            label: '工单计划开始时间',
            prop: 'planStartDate'
          },
          {
            label: '工单计划结束时间',
            prop: 'planEndDate'
          },
          {
            label: '已完工数量',
            prop: 'completedQuantity'
          },
          {
            label: '不良数',
            prop: 'ngQuantity'
          },
          {
            label: '投产数',
            prop: 'productionQuantity'
          },
          {
            label: '不良率',
            prop: 'ngRate'
          },
          {
            label: '合格率',
            prop: 'passRate'
          }
        ]
      },
      stationCrossingData: [],
      stationCrossingOption: {
        height: 200,
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        editBtn: false,
        viewBtn: false,
        selection: false,
        delBtn: false,
        addBtn: false,
        indexLabel: '序号',
        columnBtn: false,
        refreshBtn: false,
        menu: false,
        column: [
          {
            label: '工序名称',
            prop: 'routeName'
          },
          {
            label: '工序类型',
            prop: 'routeType',
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=station_type',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          },
          {
            label: '已过站数量',
            prop: 'passQuantity'
          },
          {
            label: '未过站数量',
            prop: 'waitingQuantity'
          },
          {
            label: '投产数',
            prop: 'productionQuantity'
          },
          {
            label: '不良数',
            prop: 'ngQuantity'
          },
          {
            label: '不良率',
            prop: 'ngRate'
          },
          {
            label: '合格率',
            prop: 'passRate'
          }
        ]
      },
      exportCompOption: {}
    };
  },
  computed: {
    ...mapGetters(['permission']),
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  watch: {},
  created() {},
  mounted() {
    getUserInfo().then((res) => {
      let data = res.data.data;
      let accessiblePartners = typeof data.accessiblePartners === 'string' ? JSON.parse(data.accessiblePartners) : [];
      if (accessiblePartners.length > 0) {
        this.search.partnerName = accessiblePartners[0].partnerName;
        this.partnerName = this.search.partnerName;
      }
      this.onLoad(this.page);
    });
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  methods: {
    rowClick(row) {
      this.workOrderNoData = this.workOrderNoData.map((item) => {
        if (item.workOrderId === row.workOrderId) {
          item.isCheck = true;
          this.getStationInfo(item);
        } else {
          item.isCheck = false;
        }
        return item;
      });
    },
    tableRowClassName({ row, rowIndex }) {
      console.log(row, 'row.isCheck');
      let className = row.isCheck ? 'workorderNo-select-row' : '';
      return className;
    },
    searchReset() {
      this.search = {};
      this.query = {};
      this.search.partnerName = this.partnerName;
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      if (params.planEndDate) {
        params.startTime = params.planEndDate[0];
        params.endTime = params.planEndDate[1];
        delete params.planEndDate;
      }
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      if (this.search.planEndDate) {
        params.startTime = this.search.planEndDate[0];
        params.endTime = this.search.planEndDate[1];
      }
      if (this.partnerName) {
        params.partnerName = this.partnerName;
      }
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        // this._forEach(this.data, (item) => {
        //   item.achieveRate = this._cloneDeep(item.achieveRate) + '%';
        //   item.passRate = this._cloneDeep(item.passRate) + '%';
        // });
        this.loading = false;
        this.selectionClear();
      });
    },

    viewDetail(row) {
      this.handleDialog = true;
      //   this.basicInfoForm = row;
      let params = {
        productionOrderId: row.productionOrderId
      };
      getWorkOrderNoInfo(params).then(
        (res) => {
          this.workOrderNoData = this._get(res, 'data.data', []).map((item, index) => {
            if (index === 0) {
              item.isCheck = true;
            } else {
              item.isCheck = false;
            }
            item.ngRate = this._cloneDeep(item.ngRate) + '%';
            item.passRate = this._cloneDeep(item.passRate) + '%';
            return item;
          });
          console.log(this.workOrderNoData, 'this.workOrderNoData');
          if (this.workOrderNoData.length > 0) {
            this.getStationInfo(this.workOrderNoData[0]);
          }
        },
        (error) => {
          this.workOrderNoData = [];
        }
      );
    },
    getStationInfo(row) {
      this.workOrderNoModel = row;
      getOverStationInfo({
        workOrderId: row.workOrderId
      }).then((res) => {
        this.stationCrossingData = this._get(res, 'data.data', []);
        this._forEach(this.stationCrossingData, (item) => {
          item.passRate = this._cloneDeep(item.passRate) + '%';
        });
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.titleIcon {
  display: flex;
  align-items: center;
}
.detailDialog {
  ::v-deep .el-card__body {
    .avue-crud__menu {
      display: none !important;
    }
  }
  ::v-deep .avue-crud .workorderNo-select-row {
    background-color: #caf982 !important;
  }
}
</style>
